package com.common.www.utils;

import com.alibaba.fastjson.JSON;
import com.common.www.MyApplicationListener;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import jodd.util.StringUtil;

public class JPushUtils {
    private static String APP_KEY = "d8ac8f75661b986611d1c419";
    private static String MASTER_SECRET = "4ca3c72bf7ee3d770555fb0a";

    /**
     * 推送通知到所有平台
     */
    public static boolean pushNotificationAll(String registerId, String title, String msg, HashMap hashMap) {

        //创建JPushClient
        JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY);
        //创建option
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.all())  //所有平台的用户
                .setAudience(Audience.registrationId(registerId))//registrationId指定用户
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder() //发送ios
                                .setAlert(msg) //消息体
                                .setBadge(+1)
                                .setSound("happy") //ios提示音
                                .addExtras(hashMap) //附加参数
                                .build())
                        .addPlatformNotification(AndroidNotification.newBuilder() //发送android
                                .addExtras(hashMap) //附加参数
                                .setAlert(msg) //消息体
                                .build())
                        .build())
                .setOptions(Options.newBuilder().setApnsProduction(MyApplicationListener.isRelease).build())//指定开发环境 true为生产模式 false 为测试模式 (android不区分模式,ios区分模式)
                .setMessage(Message.newBuilder().setTitle(title).setMsgContent(msg).addExtras(hashMap).build())//自定义信息
                .build();
        try {
            PushResult pu = jpushClient.sendPush(payload);
            System.out.println(pu.toString());
            return true;
        } catch (APIConnectionException e) {
            e.printStackTrace();
            return false;
        } catch (APIRequestException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 推送web通知到所有平台
     */
    public static boolean pushNotificationWebAll(String registerId, String title, String msg, String url) {
        HashMap<String, String> hashMap = new HashMap();
        hashMap.put("url", url);

        //创建JPushClient
        JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY);
        if (!StringUtil.isEmpty(hashMap.get("url"))) {
            try {
                hashMap.put("url", URLEncoder.encode(hashMap.get("url"), "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                return false;
            }
        }
        return pushNotificationAll(registerId, title, msg, hashMap);
    }

    /**
     * 推送自定义消息到所有平台
     *
     * @return 推送成功与否
     */
    public static boolean pushMsgAll(String registerId, String title, String msg, String action, Object data) {
        //添加附加参数
        HashMap hashMap = new HashMap();
        hashMap.put("msg", msg);
        hashMap.put("action", action);
        hashMap.put("data", JSON.toJSONString(data));

        //创建JPushClient
        JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY);
        //创建option
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.all())  //所有平台的用户
                .setAudience(Audience.registrationId(registerId))//registrationId指定用户
                .setOptions(Options.newBuilder().setApnsProduction(MyApplicationListener.isRelease).build())//指定开发环境 true为生产模式 false 为测试模式 (android不区分模式,ios区分模式)
                .setMessage(Message.newBuilder().setTitle(title).setMsgContent(msg).addExtras(hashMap).build())//自定义信息
                .build();
        try {
            PushResult pu = jpushClient.sendPush(payload);
            System.out.println(pu.toString());
            return true;
        } catch (APIConnectionException e) {
            e.printStackTrace();
            return false;
        } catch (APIRequestException e) {
            e.printStackTrace();
            return false;
        }
    }
}
